package com.dk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dk.pojo.TPerson;
import com.dk.pojo.TRole;
import com.dk.service.PersonService;
import com.dk.service.RoleService;
import org.apache.catalina.security.SecurityUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("personcontroller")
public class PersonController {
    @Reference
    private PersonService personService;
    @Reference
    private RoleService roleService;
    @Autowired
    private RedisTemplate redisTemplate;
    //跳转登陆页面
    @RequestMapping("toLogin")
    public String toLogin(){
        return "login";
    }

    //登录
    @RequestMapping("login")
    public String loginInto(String pname,String pwd,Model model){
        //1.获取subject对象
        Subject subject = SecurityUtils.getSubject();
        //2.封装token
        UsernamePasswordToken token = new UsernamePasswordToken(pname,pwd);
        //3.调用登陆方法
        try {
            subject.login(token);
        }catch (UnknownAccountException e){
            model.addAttribute("messg","用户名不存在");
            return "login";
        }catch (IncorrectCredentialsException e){
            model.addAttribute("messg","密码错误");
            //获取错误次数
//            String salt = (String) redisTemplate.boundHashOps("user-key").get("salt" + t.getPid());
//            if (salt == null){//如果为空 给一个0
//                redisTemplate.boundHashOps("user-key").put("salt" + t.getPid(),0+"");
//            }
//            String salt2 = (String) redisTemplate.boundHashOps("user-key").get("salt" + t.getPid());
//            int i = Integer.parseInt(salt2);
            return "login";
        }

        return "tree/show";
    }

    //跳转菜单
    @RequestMapping("totreeshow")
    public String toTreeshow(){
        return "tree/show";
    }

    //跳转用户管理页面
    @RequestMapping("toshow")
    public String toShow(){
        return "person/show";
    }

    //查询所有用户
    @RequestMapping("show")
    @ResponseBody
    public List<TPerson> selectTPerson(){
        List<TPerson> list = personService.selectTPerson();
        return list;
    }

    //跳转用户修改页面
    @RequestMapping("toupdate")
    public String toUpdate(Integer id, Model model){
        //查询当前用户对象
        TPerson tPerson = personService.selectById(id);
        model.addAttribute("tperson",tPerson);
        //查询所有角色
        List<TRole> tRoles = roleService.selectRole();
        model.addAttribute("troles",tRoles);
        //查询当前用户的所有角色
        List<Integer> pids = personService.selectByPid(id);
        model.addAttribute("pids",pids);
        return "person/update";
    }

    //用户修改
    @RequestMapping("updateinto")
    @ResponseBody
    public Boolean updateInto(TPerson tPerson,Integer[] roleId){
        Boolean b = personService.updateInto(tPerson,roleId);
        return b;
    }

    //删除用户
    @RequestMapping("deleteById")
    @ResponseBody
    public Boolean deleteById(Integer id){
        Boolean b = personService.deleteById(id);
        return b;
    }
}
